package datenbank;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class ModelStat6 implements TableModel {

	private DatenbVerb datenbVerb;
	private final String TABELLE = "station6";
	private String artNr;
	private List<TableModelListener> tableListener = new ArrayList<TableModelListener>();

	/**
	 * Konstruktor
	 * 
	 * @param datenbVerb
	 */
	public ModelStat6(DatenbVerb datenbVerb) {
		this.datenbVerb = datenbVerb;
	}

	/**
	 * 
	 */
	public void addTableModelListener(TableModelListener arg0) {
		tableListener.add(arg0);
	}

	/**
	 * 
	 */
	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
		case 0:
			return String.class;
		case 1:
			return String.class;
		case 2:
			return Object.class;
		}
		return null;
	}

	/**
	 * 
	 */
	public int getColumnCount() {
		return 3;
	}

	/**
	 * 
	 */
	public String getColumnName(int columnIndex) {
		switch (columnIndex) {
		case 0:
			return "Index";
		case 1:
			return "Beschreibung";
		case 2:
			return "Wert";
		}
		return null;
	}

	/**
	 * 
	 */
	public int getRowCount() {
		return datenbVerb.getSize(TABELLE, artNr, 2) - 4;
	}

	/**
	 * 
	 */
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			if (rowIndex > 19) {
				return String.valueOf(rowIndex - 20) + " (Rob Real)";
			}

			if (rowIndex > 9) {
				return String.valueOf(rowIndex - 10) + " (SPS Real)";
			}
			return String.valueOf(rowIndex) + " (SPS Int)";
		}

		return datenbVerb.getAbfrageKompl(TABELLE, artNr, rowIndex, rowIndex * 2 + columnIndex + 1);
	}

	/**
	 * 
	 */
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return false;

		default:
			return true;
		}
	}

	/**
	 * 
	 */
	public void removeTableModelListener(TableModelListener arg0) {
		tableListener.remove(arg0);
	}

	/**
	 * 
	 */
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 1:
			datenbVerb.setUpdateString(TABELLE, artNr, rowIndex, rowIndex * 2 + columnIndex + 1, value);
			break;
		case 2:
			datenbVerb.setUpdateZahl(TABELLE, artNr, rowIndex, rowIndex * 2 + columnIndex + 1, value);
			break;
		}
	}

	/**
	 * 
	 * @param artNr
	 * @param standard
	 */
	public void setNewArtikel(String artNr, boolean standard) {
		if (datenbVerb.getArtikelVorh(TABELLE, artNr)) {
			System.out.println("Artikel vorhanden");
		} else {
			this.artNr = artNr;

			this.datenbVerb.setNewArtikel(artNr, TABELLE);

			if (standard) {
				// datenbVerb.copyOf(TABELLE, artNr, "standard");
			}

			for (TableModelListener l : tableListener)
				l.tableChanged(new TableModelEvent(this));
		}
	}

	/**
	 * 
	 * @param artNr
	 * @return Artikel geladen
	 */
	public boolean setLoadArtikel(String artNr) {
		if (datenbVerb.getArtikelVorh(TABELLE, artNr)) {
			this.artNr = artNr;

			for (TableModelListener l : tableListener)
				l.tableChanged(new TableModelEvent(this));

			return true;
		}

		return false;
	}

	/**
	 * 
	 * @param columnIndex
	 * @return Object
	 */
	public Float getDBEintragFloat(int columnIndex) {
		Object o = datenbVerb.getDBeintrag(TABELLE, artNr, columnIndex);

		if (o == null) {
			return 0.0f;
		}

		return Float.parseFloat(String.valueOf(o));
	}

	/**
	 * 
	 * @param columnIndex
	 * @return Object
	 */
	public Integer getDBEintragInt(int columnIndex) {
		Object o = datenbVerb.getDBeintrag(TABELLE, artNr, columnIndex);

		if (o == null) {
			return 0;
		}

		return (Integer) o;
	}

	/**
	 * 
	 * @param columnIndex
	 * @return Object
	 */
	public boolean getDBEintragBit(int columnIndex) {
		Object o = datenbVerb.getDBeintrag(TABELLE, artNr, columnIndex);

		if (o == null) {
			return false;
		}

		return (boolean) o;
	}

	/**
	 * 
	 * @param copyof
	 * @return boolean
	 */
	public boolean artikelCopy(String copyof) {
		if (!datenbVerb.getArtikelVorh(TABELLE, copyof) && !artNr.equals("")) {
			return false;
		}

		datenbVerb.copyOf(TABELLE, artNr, copyof);

		for (TableModelListener l : tableListener) {
			l.tableChanged(new TableModelEvent(this));
		}

		return true;
	}
}